package com.shu.framework.utils;

import com.alibaba.fastjson.JSON;

public class JsonUtil {

    /**
     * 对象转json
     * @param value
     * @param <T>
     * @return
     */
    public static <T> String convertObj2String(T value) {
        Class<?> clazz=value.getClass();
        if (clazz == int.class || clazz == Integer.class){
            return ""+value;
        }else  if (clazz == String.class){
            return  (String) value;
        }else  if (clazz == long.class || clazz == Long.class){
            return ""+value;
        }else {
            return JSON.toJSONString(value);
        }
    }

    /**
     * json转对象
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T convertString2Obj(String str,Class<T> clazz) {

        if (clazz == int.class || clazz == Integer.class){
            return (T)Integer.valueOf(str);
        }else  if (clazz == String.class){
            return  (T)str;
        }else  if (clazz == long.class || clazz == Long.class){
            return (T)Long.valueOf(str);
        }else {
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }
}
